package com.lkhhd.category.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lkhd.category.service.CategoryService;
import com.lkhd.common.myemum.CategoryEnum;
import com.lkhd.common.utils.IDUtil;
import com.lkhd.manage.mapper.ActivityMapper;
import com.lkhd.manage.mapper.ActivitymultipartcategoryMapper;
import com.lkhd.manage.mapper.AttributenameMapper;
import com.lkhd.manage.mapper.CategoryMapper;
import com.lkhd.manage.pojo.Activity;
import com.lkhd.manage.pojo.Activitymultipartcategory;
import com.lkhd.manage.pojo.Attributename;
import com.lkhd.manage.pojo.Category;
import com.lkhd.manage.povo.CategoryVo;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService{
	
	@Autowired
	private CategoryMapper categoryMapper;
	@Autowired
	private AttributenameMapper attributenameMapper;
	@Autowired
	private ActivitymultipartcategoryMapper activitymultipartcategoryMapper;
	@Autowired
	private ActivityMapper activityMapper;
	/**
	 * 	添加分类
	 * @param category 分类信息
	 * @param attrNames	分类的属性名
	 * @return
	 */
	@Override
	public int addCategory(Category category, List<Attributename> attrNames) {
		category.setCategoryid(IDUtil.getId());
		category.setStatus(CategoryEnum.UP.getCode());
		//插入分类
		 int insertCategory = categoryMapper.insert(category);
		// 插入分类下的属性名
		 for(Attributename attr : attrNames) {
			 attr.setAttributenameid(IDUtil.getId());
			 attr.setCategoryid(category.getCategoryid());
			 attributenameMapper.insert(attr);
		 }
		 return insertCategory;
	}

	/**
	 * 	更新分类
	 * @param category
	 * @return
	 */
	@Override
	public int update(Category category) {
		return categoryMapper.updateByPrimaryKeySelective(category);
	}

	/**
	 *   下架分类(分类下的活动全部下架)
	 * @param CategoryId
	 * @param is   True 下架 false上架
	 * @return
	 */
	@Override
	public int freeze(Long categoryId,boolean is) {
		int status = is==true?CategoryEnum.DOWN.getCode():CategoryEnum.UP.getCode();
		// 查询哪些活动是该分类的
		Activitymultipartcategory activitymultipartcategory = new Activitymultipartcategory();
		activitymultipartcategory.setCategoryid(categoryId);
		List<Activitymultipartcategory> amcs = activitymultipartcategoryMapper.select(activitymultipartcategory);
		// 把所属活动下架
		Activity activity = new Activity();
		for(Activitymultipartcategory amc : amcs) {
			activity.setActivityid(amc.getActivityid());
			activity.setStatus(status);
			activityMapper.updateByPrimaryKeySelective(activity);
		}
		Category category = new Category();
		category.setCategoryid(categoryId);
		category.setStatus(status);
		return categoryMapper.updateByPrimaryKeySelective(category);
	}

	/**
	 *   添加属性名
	 * @param CategoryId
	 * @param attributeName
	 * @return
	 */
	@Override
	public int addAttributeName(Attributename attributeName) {
		return attributenameMapper.insert(attributeName);
	}

	/**
	 *  		删除属性名
	 * @param attributeNameId
	 * @return
	 */
	@Override
	public int delAttributeName(Long attributeNameId) {
		return attributenameMapper.deleteByPrimaryKey(attributeNameId);
	}

	/**
	 * 	更新属性名
	 * @param attr
	 * @return
	 */
	@Override
	public int updateAttrbuteName(Attributename attr) {
		return attributenameMapper.updateByPrimaryKeySelective(attr);
	}

	/**
	 *   根据分类id查询分类下的分类属性名
	 * @param categoryId
	 * @return
	 */
	@Override
	public List<Attributename> selectAtrributeNameForCategoryId(Long categoryId) {
		Example example = new Example(Attributename.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("categoryid", categoryId);
		
		return attributenameMapper.selectByExample(example);
	}

	/**
	 *   查询所有分类[包括属性名]
	 * @return
	 */
	@Override
	public List<CategoryVo> selectAll(Example example) {
		List<CategoryVo> vategoryVOs = new ArrayList<>();
		
		List<Category> categorys = categoryMapper.selectByExample(example);
		for(Category category : categorys) {
			// 封装分类信息
			CategoryVo categoryVo = new CategoryVo();
			vategoryVOs.add(categoryVo);
			BeanUtils.copyProperties(category, categoryVo);
			// 设置属性名集合
			List<Attributename> attrs = this.selectAtrributeNameForCategoryId(category.getCategoryid());
			categoryVo.setAttrs(attrs);
		}
		
		return vategoryVOs;
	}

}
